package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerForwardOrderActivityComponent;
import com.wgts.wgts.dagger.module.ForwardOrderActivityModule;
import com.wgts.wgts.localbean.LocalMoneyRecordProgress;
import com.wgts.wgts.mvp.presenter.IForwardOrderActivityPresenter;
import com.wgts.wgts.mvp.view.IForwardOrderActivityView;
import com.wgts.wgts.netmodle.resp.UserMoneyRecordEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForwardOrderActivity extends BaseActivity implements IForwardOrderActivityView {
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;

    @Inject
    IForwardOrderActivityPresenter presenter;
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;

    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;
    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.net_error_ly)
    LinearLayout netErrorLy;

    private ForwardOrderAdapter adapter;

    private List<UserMoneyRecordEntity> entities;


    @Override
    public void init() {
        DaggerForwardOrderActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .forwardOrderActivityModule(new ForwardOrderActivityModule(this))
                .build()
                .inject(this);
        entities = new ArrayList<>();
    }

    private void initView() {
        includeTitle.setText("提现记录");
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ForwardOrderAdapter(this, entities);
        adapter.setOnClickListener(new ForwardOrderAdapter.OnClickListener() {
            @Override
            public void onClick(View view, int i) {

                ArrayList<LocalMoneyRecordProgress> progressArrayList = new ArrayList<>();

                for (UserMoneyRecordEntity.Progress item : entities.get(i).progress) {
                    LocalMoneyRecordProgress progress = new LocalMoneyRecordProgress();
                    progress.msg = item.msg;
                    progress.state = entities.get(i).status;
                    progress.stateTime = item.stateTime;
                    progressArrayList.add(progress);
                }

                Intent intent = new Intent(ForwardOrderActivity.this, PutForwardProgressActivity.class);
                intent.putParcelableArrayListExtra(PutForwardProgressActivity.PROGRESS, progressArrayList);
                startActivity(intent);
            }
        });

        smartRefreshLayout.setEnableLoadmore(true);
        smartRefreshLayout.setEnableRefresh(true);

        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.loadPutAwardOrder();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                presenter.initPutAwardOrder();
            }
        });

        recycleView.setAdapter(adapter);

        presenter.initPutAwardOrder();
    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.forward_order_activity);
        ButterKnife.bind(this);
        initView();
    }


    @Override
    public void initRecodList(List<UserMoneyRecordEntity> entity, boolean noMore) {
        if (entity.size() == 0) {
            noneDataLy.setVisibility(View.VISIBLE);
        } else {
            noneDataLy.setVisibility(View.GONE);
        }
        smartRefreshLayout.finishRefresh();
        entities.clear();
        entities.addAll(entity);
        adapter.setNoMore(noMore);
        adapter.notifyDataSetChanged();
        smartRefreshLayout.setEnableLoadmore(!noMore);
    }

    @Override
    public void loadRecodList(List<UserMoneyRecordEntity> entity, boolean noMore) {
        smartRefreshLayout.finishLoadmore();
        smartRefreshLayout.setEnableLoadmore(!noMore);
        entities.addAll(entity);
        adapter.setNoMore(noMore);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onError(int status, String msg) {
        finish();
    }

    @OnClick({R.id.back_btn_image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_image:
                finish();
                break;
        }
    }

}
